"""

@author: lingw
@email: gw.lin@hzgosun.com
@file: cameraListTest.py.py
@time: 2019-5-20 下午 3:56

这一行开始写关于本文件的说明与解释

"""

import unittest

from time import sleep
import os

from selenium.webdriver.common.keys import Keys

from common.decorators import mkdir_classname,log4gs
from common.functions import save_screenshot
from common.settings import TAG, DATA_DIR
from common.vue.select import DropDownSelect, OperateSelect
from pages.camListPage import CamListPage


@log4gs
@mkdir_classname(TAG)
class CamListTest(unittest.TestCase):
    """摄像机管理功能测试"""
    @classmethod
    def setUpClass(cls):
        cls.log.info('=' * 30 + "开始摄像机管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page = CamListPage('/DevManage/camManage')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束摄像机管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page.driver.quit()

    def setUp(self):
        self.log.info(">>>>>>开始执行用例:{} {}".format(self.id(), self.shortDescription()))

    def tearDown(self):
        self.log.info("<<<<<<结束执行用例:{} {}".format(self.id(), self.shortDescription()))

    @unittest.skip
    def test_1_turn_page(self):
        """翻页功能"""
        self.log.debug("当前共有：" + self.page.turnPage.total_page +"页数据" )
        self.log.debug("当前处于第：" + self.page.turnPage.current_page + "页数据")
        self.assertEqual(self.page.turnPage.current_page, '1')
        sleep(1)
        try:
            self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')

            sleep(1)
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '3')

            sleep(1)
            self.page.turnPage.prev_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')

            sleep(1)
            self.page.turnPage.input.clear()
            self.page.turnPage.input.send_keys(6)
            self.page.turnPage.input.send_keys(Keys.ENTER)
            self.assertEqual(self.page.turnPage.current_page, '6')
        except Exception:
            self.log.error("当前模块最大为1页，无法进行翻页测试", exc_info=True)

    @unittest.skip
    def test_2_search_keywords(self):
        """查询功能测试
        http://181.181.0.216:8786/tc/detail/74-3777/
        http://181.181.0.216:8786/tc/detail/74-3778/
        http://181.181.0.216:8786/tc/detail/74-3779/
        """

        search_test_list = [
            {
                'condition':"摄像机名称",
                'input':"金海5",
                'expectation':"金海5"
            },
            {
                'condition': "摄像机IP",
                'input': "3.1.1.18",
                'expectation': "3.1.1.18"
            },
            {
                'condition': "摄像机编码",
                'input': "TLSSJ0011",
                'expectation': "TLSSJ0011"
            }
        ]

        for item in search_test_list:
            condition = item.get('condition')
            input = item.get('input')
            expectation = item.get('expectation')
            ### 利用subTest对这种只是输入数据不一样，步骤一样的用例，进行统一测试
            with self.subTest(**item):
                self.log.info("用以下数据进行搜索测试：\n{}".format(item))
                DropDownSelect(self.page.searchTools.keyword_dropdown_list, condition)
                self.page.searchTools.keyword_input.send_keys(input)
                self.page.searchTools.search_btn.click()
                sleep(1)
                try:
                    result = self.page.table.get_row(expectation)
                    self.assertTrue(result, msg="没有找到结果：{}".format(expectation))
                    self.log.info("用例‘{}’执行成功".format(self.id()))
                except Exception:
                    self.log.error("用例‘{}’执行失败".format(self.id()), exc_info=True)
                self.page.searchTools.keyword_input.clear()

    @unittest.skip
    def test_3_search_region(self):
        """区域、小区查询
        http://181.181.0.216:8786/tc/detail/74-3782/
        """

        self.page.searchTools.region_select_btn.click()
        self.page.searchTools.region_selector.select("广西壮族自治区-南宁市-青秀区")
        DropDownSelect(self.page.searchTools.communicity_dropdown_list, "屯里小区")
        self.page.searchTools.keyword_input.clear()
        self.page.searchTools.search_btn.click()
        sleep(2)
        try:
            result = self.page.table.get_row("屯里摄像机0001")
            self.assertTrue(result)
            self.log.info("在广西壮族自治区-南宁市-青秀区下的屯里小区找到了摄像机：屯里摄像机0001")
            # save_screenshot(self, "查询摄像机成功截图")
        except Exception:
            self.log.error("在广西壮族自治区-南宁市-青秀区下的屯里小区找不到摄像机：屯里摄像机0001", exc_info=True)
            save_screenshot(self, "查询摄像机失败截图")
        self.page.searchTools.keyword_input.clear()
        self.page.searchTools.region_select_btn.clear()
        self.page.searchTools.communicity_dropdown_list.clear()

    @unittest.skip
    def test_4_add_camera(self):
        """增加摄像机
        http://181.181.0.216:8786/tc/detail/74-3735/
        """
        self.page.table.add_btn.click()
        self.page.addCam.camera_account.send_keys("admin")
        self.page.addCam.password.send_keys("22222")
        self.page.addCam.camera_number.send_keys("test123456")
        self.page.addCam.cameraIP.send_keys("9.9.9.9")

        DropDownSelect(self.page.addCam.camera_type, "普通摄像机")
        self.page.addCam.camera_name.send_keys("测试摄像机名称")

        self.page.addCam.region_add_btn.click()
        sleep(1)
        self.page.addCam.region_add_selector.select("广西壮族自治区-南宁市-青秀区")
        # self.page.addCam.region_add_selector.select("北京-北京市-东城区")
        # self.page.addCam.region_add_selector.select("内蒙古自治区-包头市-东河区")
        # self.page.addCam.region_add_selector.select("浙江省-杭州市-江干区")
        # self.page.addCam.region_add_selector.select("澳门特别行政区-澳门半岛-大堂区")
        sleep(1)
        DropDownSelect(self.page.addCam.communicity_list, "屯里小区")
        sleep(1)
        DropDownSelect(self.page.addCam.bind_electricity_fence, "屯里电围0001")

        #如果输入平台id会导致页面报错：相机的平台相关参数配置不全，所以注释掉这行代码
        #而且平台id这个输入项暂时无用，后期会去除
        # self.page.addCam.platform_id.send_keys("9.9.9.9")

        sleep(1)
        DropDownSelect(self.page.addCam.manufacturer_type, "大华")
        sleep(1)
        DropDownSelect(self.page.addCam.camera_model, "DH-IPC-HF8229F")
        sleep(1)
        DropDownSelect(self.page.addCam.camera_face, "东")

        self.page.addCam.snap_the_alarm_cap.send_keys("9999999")
        self.page.addCam.capture_the_lower_alarm_limit.send_keys("999")
        self.page.addCam.GIS.send_keys("120.288159,30.335993")

        self.page.addCam.confirm.click()
        sleep(2)

        DropDownSelect(self.page.searchTools.keyword_dropdown_list, "摄像机名称")
        self.page.searchTools.keyword_input.clear()
        self.page.searchTools.keyword_input.send_keys("测试摄像机名称")
        self.page.searchTools.search_btn.click()
        save_screenshot(self,"新增摄像机截图")
        sleep(2)

        try:
            result = self.page.table.get_row("测试摄像机名称")
            self.assertTrue(result, msg="成功查询到该新加摄像机，添加成功")
            self.log.info("成功查询到该新加摄像机，用例执行成功")
        except Exception:
            self.log.error("无法查找到该摄像机，用例执行失败", exc_info=True)
            save_screenshot(self, "无法找到新增的摄像机")


    @unittest.skip
    def test_5_modify_camera(self):
        """修改摄像机
        http://181.181.0.216:8786/tc/detail/74-3762/
        """

        DropDownSelect(self.page.searchTools.keyword_dropdown_list, "摄像机名称")
        self.page.searchTools.keyword_input.clear()
        self.page.searchTools.keyword_input.send_keys("测试摄像机名称")
        self.page.searchTools.search_btn.click()
        sleep(2)

        search_result = self.page.table.get_row("测试摄像机名称")
        sleep(2)
        OperateSelect(search_result, "查看")
        # OperateSelect(search_result, btn_index=0)
        sleep(2)

        self.page.modifyCam.modify_btn.click()
        self.page.modifyCam.camera_name.clear()
        self.page.modifyCam.camera_name.send_keys("测试修改摄像机名称")
        self.page.modifyCam.confirm.click()

        sleep(2)
        DropDownSelect(self.page.searchTools.keyword_dropdown_list, "摄像机名称")
        self.page.searchTools.keyword_input.clear()
        self.page.searchTools.keyword_input.send_keys("测试修改摄像机名称")
        self.page.searchTools.search_btn.click()

        sleep(2)
        modify_result = self.page.table.get_row("测试修改摄像机名称")
        try:
            self.assertTrue(modify_result, msg="无法查找到修改后的摄像机名称，修改失败")
            self.log.info("修改摄像机名称成功")
        except Exception:
            self.log.error("修改摄像机名称失败", exc_info=True)


    @unittest.skip
    def test_6_delect_camera(self):
        """删除摄像机
        http://181.181.0.216:8786/tc/detail/74-3775/
        """

        DropDownSelect(self.page.searchTools.keyword_dropdown_list, "摄像机名称")
        self.page.searchTools.keyword_input.clear()
        self.page.searchTools.keyword_input.send_keys("测试修改摄像机名称")
        self.page.searchTools.search_btn.click()

        sleep(2)
        result = self.page.table.get_row("测试修改摄像机名称")
        OperateSelect(result, "删除")

        sleep(2)
        # self.page.deleteCam.delect_btn.click()
        self.page.deleteCam.confirm.click()

        sleep(2)
        DropDownSelect(self.page.searchTools.keyword_dropdown_list, "摄像机名称")
        self.page.searchTools.keyword_input.clear()
        self.page.searchTools.keyword_input.send_keys("测试修改摄像机名称")
        self.page.searchTools.search_btn.click()

        sleep(2)
        delete_result = self.page.table.get_row("测试修改摄像机名称")
        try:
            self.assertFalse(delete_result, msg="摄像机删除失败，依旧存在于列表中")
            self.log.info("删除摄像机成功")
        except Exception:
            self.log.error("删除摄像机失败", exc_info=True)

    # @unittest.skip
    def test_7_import_cameras(self):
        """批量导入摄像机
        http://181.181.0.216:8786/tc/detail/74-3759/
        """

        self.page.table.import_btn.click()
        self.page.importCam.select_imput.send_keys(os.path.join(DATA_DIR, "测试导入摄像机.xlsx"))
        self.page.importCam.imput_btn.click()
        sleep(2)

        self.page.importCam.close()
        sleep(1)

        DropDownSelect(self.page.searchTools.keyword_dropdown_list, '摄像机编码')
        sleep(1)

        self.page.searchTools.keyword_input.send_keys('TEST0001')
        self.page.searchTools.search_btn.click()
        sleep(1)

        search_result_1 = self.page.table.get_row('TEST0001')

        try:
            self.assertTrue(search_result_1, msg='批量导入第一个测试相机失败')
            self.log.info('批量导入第一个摄像机成功')
        except Exception:
            self.log.error('批量导入第一个摄像机失败', exc_info=True)
            save_screenshot('批量导入第一个摄像机失败')
        sleep(1)

        OperateSelect(search_result_1, '删除')
        sleep(1)

        self.page.deleteCam.confirm.click()
        sleep(1)

        self.page.searchTools.keyword_input.clear()
        self.page.searchTools.keyword_input.send_keys('TEST0002')
        self.page.searchTools.search_btn.click()
        sleep(1)


        search_result_2 = self.page.table.get_row('TEST0002')
        try:
            self.assertTrue(search_result_2, msg='批量导入第二个测试相机失败')
            self.log.info('批量导入第二个摄像机成功')
            self.log.info('批量导入全部摄像机成功')
        except Exception:
            self.log.error('批量导入第二个摄像机失败', exc_info=True)
            save_screenshot('批量导入第二个摄像机失败')
        sleep(1)

        OperateSelect(search_result_2, '删除')
        sleep(1)

        self.page.deleteCam.confirm.click()
        sleep(1)


if __name__ == '__main__':
    unittest.main()




